package com.crack.nga2.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.crack.nga2.R;
import com.crack.nga2.activity.userCenter.HistoryBrowseActivity;
import com.crack.nga2.activity.userCenter.UserCollectActivity;
import com.crack.nga2.activity.userCenter.UserInfoActivity;
import com.crack.nga2.activity.userCenter.UserReplyActivity;
import com.crack.nga2.activity.userCenter.UserThemeActivity;
import com.crack.nga2.utils.GlobalConst;

import java.io.File;

public class UserCenterActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    private int[] imgs = {R.drawable.user_center_icon, R.drawable.my_theme,
            R.drawable.my_reply, R.drawable.favorite, R.drawable.history, R.drawable.exit};
    private String[] items;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_center);
        items = getResources().getStringArray(R.array.user_center);
        ListView listView = (ListView) findViewById(R.id.listView);
        UserCenterAdapter adapter = new UserCenterAdapter();
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }

    public void doClick(View view) {
        switch (view.getId()) {
            case R.id.action_return:
                finishForAnimation();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position){
            case 0:
                //跳转到个人信息
                Intent intent = new Intent(this, UserInfoActivity.class);
                startActivity(intent);
                break;
            case 1:
                //我的主题
                intent = new Intent(this, UserThemeActivity.class);
                startActivity(intent);
                break;
            case 2:
                //我的回复
                intent = new Intent(this, UserReplyActivity.class);
                startActivity(intent);
                break;
            case 3:
                //我的收藏
                intent = new Intent(this,UserCollectActivity.class);
                startActivity(intent);
                break;
            case 4:
                intent = new Intent(this, HistoryBrowseActivity.class);
                startActivity(intent);
                break;
            case 5:
                //退出当前账号 TODO
                File file = new File(getFilesDir(),"cookie.cache");
                if(file.delete()) {
                    Toast.makeText(this,"已退出",Toast.LENGTH_SHORT).show();
                    GlobalConst.isLogin = false;
                    GlobalConst.Cookie = "";
                    setResult(2);
                    finish();
                }
        }
    }

    private class UserCenterAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return items.length < imgs.length ? items.length : imgs.length;
        }

        @Override
        public Object getItem(int position) {
            return items[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            @SuppressLint("ViewHolder")
            View view = View.inflate(UserCenterActivity.this, R.layout.activity_user_center_item, null);
            ImageView img = (ImageView) view.findViewById(R.id.icon);
            TextView text = (TextView) view.findViewById(R.id.text);
            img.setImageResource(imgs[position]);
            text.setText(items[position]);
            return view;
        }
    }
}
